/*******************************************************
> Copyright (C) 2024 ==iot== All rights reserved.
> File Name: client.c
> Author: wyf
> Created Time: 2024年08月13日 星期二 13时44分07秒
***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> //目录名获取配合DIR
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> //文件控制操作
#include <sys/types.h> //获取pid等
#include <sys/wait.h> //等待子进程结束
#include <time.h> //时间获取
#include <sys/ipc.h> //进程通信
#include <sys/msg.h> //消息队列
#include <sys/shm.h> //共享内存
#include <signal.h> //信号处理
#include <pthread.h> //线程
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

struct head
{
	int type;
	char mean[10];
};

struct exam
{
	int data1;
	int data2;
};
struct result
{
	int res;
};

int main(int argc, const char* argv[])
{
	int sock = 0;
	int ret = 0;
	int op = 0;

	char buf[1024] = { 0 };
	struct sockaddr_in serAddr;
	memset(&serAddr, 0, sizeof(serAddr));

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
	{
		perror("sock");
		exit(-1);
	}

	serAddr.sin_family = AF_INET;
	serAddr.sin_port = 5494;
	serAddr.sin_addr.s_addr = inet_addr("192.168.48.128");
	struct head hd;
	struct exam ex;
	struct result re;
	printf("加1，减2，乘3，除4:\n");
	scanf("%d", &op);
	switch (op)
	{
	case 1:	//加法
		hd.type = 1;
		hd.mean[0] = '+';
		printf("请输入第一个数：\n");
		scanf("%d", &ex.data1);
		printf("请输入第二个数：\n");
		scanf("%d", &ex.data2);

		memcpy(buf, &hd, sizeof(struct head));
		memcpy(buf + sizeof(hd), &ex, sizeof(struct exam));
		ret = sendto(sock, buf, sizeof(struct head) + sizeof(struct exam), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));
		if (ret == -1)
		{
			perror("sendto");
			exit(-1);
		}
		printf("加法---发送成功\n");

		ret = recvfrom(sock, &re, sizeof(struct result), 0, NULL, NULL);
		printf("结果是：%d", re.res);
		break;


	case 2:
		hd.type = 2;
		hd.mean[0] = '-';
		printf("请输入第一个数：\n");
		scanf("%d", &ex.data1);
		printf("请输入第二个数：\n");
		scanf("%d", &ex.data2);

		memcpy(buf, &hd, sizeof(struct head));
		memcpy(buf + sizeof(hd), &ex, sizeof(struct exam));
		ret = sendto(sock, buf, sizeof(struct head) + sizeof(struct exam), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));
		if (ret == -1)
		{
			perror("sendto");
			exit(-1);
		}
		printf("减法---发送成功\n");
		ret = recvfrom(sock, &re, sizeof(struct result), 0, NULL, NULL);
		printf("结果是：%d\n", re.res);
		break;
	
	case 3:
		hd.type = 3;
		hd.mean[0] = '*';
		printf("请输入第一个数：\n");
		scanf("%d", &ex.data1);
		printf("请输入第二个数：\n");
		scanf("%d", &ex.data2);

		memcpy(buf, &hd, sizeof(struct head));
		memcpy(buf + sizeof(hd), &ex, sizeof(struct exam));
		ret = sendto(sock, buf, sizeof(struct head) + sizeof(struct exam), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));
		if (ret == -1)
		{
			perror("sendto");
			exit(-1);
		}
		printf("乘法---发送成功\n");
		ret = recvfrom(sock, &re, sizeof(struct result), 0, NULL, NULL);
		printf("结果是：%d\n", re.res);
		break;

	case 4:
		hd.type = 4;
		hd.mean[0] = '/';
		printf("请输入第一个数：\n");
		scanf("%d", &ex.data1);
		printf("请输入第二个数：\n");
		scanf("%d", &ex.data2);

		memcpy(buf, &hd, sizeof(struct head));
		memcpy(buf + sizeof(hd), &ex, sizeof(struct exam));
		ret = sendto(sock, buf, sizeof(struct head) + sizeof(struct exam), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));
		if (ret == -1)
		{
			perror("sendto");
			exit(-1);
		}
		printf("除法---发送成功\n");
		ret = recvfrom(sock, &re, sizeof(struct result), 0, NULL, NULL);
		printf("结果是：%d\n", re.res);
		break;
	}

	close(sock);
    return 0;

}


